<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="从0-1搭建一个vue3项目, W.BOWEN&#39;s blog">
    <meta name="description" content="接受失败,但不选择放弃">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>从0-1搭建一个vue3项目 | W.BOWEN&#39;s blog</title>
    <link rel="icon" type="image/png" href="/wangbowen-hexo-blog/favicon.png">

    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/css/my.css">

    <script src="/wangbowen-hexo-blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.3.0"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/wangbowen-hexo-blog/" class="waves-effect waves-light">
                    
                    <img src="/wangbowen-hexo-blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">W.BOWEN&#39;s blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wangbowen-hexo-blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/wangbowen-hexo-blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">W.BOWEN&#39;s blog</div>
        <div class="logo-desc">
            
            接受失败,但不选择放弃
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wangbowen-hexo-blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://gitee.com/wang-bowen" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://gitee.com/wang-bowen" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/wangbowen-hexo-blog/medias/featureimages/1.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">从0-1搭建一个vue3项目</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/wangbowen-hexo-blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/wangbowen-hexo-blog/tags/vue/">
                                <span class="chip bg-color">vue</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2024-03-06
                </div>
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="搭建"><a href="#搭建" class="headerlink" title="搭建"></a>搭建</h2><h4 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h4><p>首先创建一个项目文件夹，通过<code>npm init</code>创建一个 package.json 文件</p>
<p><img src="image-2.png" alt="Alt text"></p>
<p>然后在项目下创建相关文件夹</p>
<p><img src="vue%E6%96%87%E4%BB%B6%E5%A4%B9%E5%90%AB%E4%B9%89.jpg" alt="Alt text"></p>
<h4 id="安装相关依赖"><a href="#安装相关依赖" class="headerlink" title="安装相关依赖"></a>安装相关依赖</h4><ul>
<li>安装 axios 库<br>npm install axios</li>
<li>安装 pinia 状态管理库<br>npm install pinia</li>
<li>安装 vue 相关依赖<br>npm install vue vue-router</li>
<li>安装进度条插件<br>npm install nprogress</li>
<li>安装 pinia 状态管理库持久管理插件 pinia-plugin-persistedstate<br>npm install pinia-plugin-persistedstate</li>
<li>安装组件库 elementPlus<br>npm install element-plus</li>
<li>安装 typescript、less<br>npm install typescript less -D</li>
</ul>
<h4 id="新建文件"><a href="#新建文件" class="headerlink" title="新建文件"></a>新建文件</h4><ul>
<li><p>新建<code>index.html</code>文件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 引入main.ts入口文件 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;module&quot;</span> <span class="attr">src</span>=<span class="string">&quot;./src/main.ts&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>在 src 文件夹下新建入口文件<code>main.ts</code></p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&quot;./styles/reset.less&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&quot;./App.vue&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&quot;./router&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> pinia <span class="keyword">from</span> <span class="string">&quot;./store&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> app = createApp(App);</span><br><span class="line">app.use(router).use(pinia);</span><br><span class="line">app.mount(<span class="string">&quot;#app&quot;</span>);</span><br></pre></td></tr></table></figure></li>
<li><p>在 src 文件夹下新建<code>App.vue</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;!-- 入口文件 --&gt;</span><br><span class="line">  &lt;router-view&gt;&lt;&#x2F;router-view&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang&#x3D;&quot;ts&quot; setup&gt;</span><br><span class="line">import &#123; ref, reactive &#125; from &quot;vue&quot;;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang&#x3D;&quot;less&quot; scoped&gt;&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure></li>
<li><p>在 styles 文件夹下新建<code>reset.less</code>文件</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">html</span>,</span><br><span class="line"><span class="selector-tag">body</span>,</span><br><span class="line"><span class="selector-tag">p</span>,</span><br><span class="line"><span class="selector-tag">ol</span>,</span><br><span class="line"><span class="selector-tag">ul</span>,</span><br><span class="line"><span class="selector-tag">li</span>,</span><br><span class="line"><span class="selector-tag">dl</span>,</span><br><span class="line"><span class="selector-tag">dt</span>,</span><br><span class="line"><span class="selector-tag">dd</span>,</span><br><span class="line"><span class="selector-tag">blockquote</span>,</span><br><span class="line"><span class="selector-tag">figure</span>,</span><br><span class="line"><span class="selector-tag">fieldset</span>,</span><br><span class="line"><span class="selector-tag">legend</span>,</span><br><span class="line"><span class="selector-tag">textarea</span>,</span><br><span class="line"><span class="selector-tag">pre</span>,</span><br><span class="line"><span class="selector-tag">iframe</span>,</span><br><span class="line"><span class="selector-tag">hr</span>,</span><br><span class="line"><span class="selector-tag">h1</span>,</span><br><span class="line"><span class="selector-tag">h2</span>,</span><br><span class="line"><span class="selector-tag">h3</span>,</span><br><span class="line"><span class="selector-tag">h4</span>,</span><br><span class="line"><span class="selector-tag">h5</span>,</span><br><span class="line"><span class="selector-tag">h6</span> &#123;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">h1</span>,</span><br><span class="line"><span class="selector-tag">h2</span>,</span><br><span class="line"><span class="selector-tag">h3</span>,</span><br><span class="line"><span class="selector-tag">h4</span>,</span><br><span class="line"><span class="selector-tag">h5</span>,</span><br><span class="line"><span class="selector-tag">h6</span> &#123;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">font-weight</span>: normal;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">ul</span> &#123;</span><br><span class="line">  <span class="attribute">list-style</span>: none;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">button</span>,</span><br><span class="line"><span class="selector-tag">input</span>,</span><br><span class="line"><span class="selector-tag">select</span> &#123;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">html</span> &#123;</span><br><span class="line">  <span class="attribute">box-sizing</span>: border-box;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">*,</span><br><span class="line">*<span class="selector-pseudo">::before</span>,</span><br><span class="line">*<span class="selector-pseudo">::after</span> &#123;</span><br><span class="line">  <span class="attribute">box-sizing</span>: inherit;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">img</span>,</span><br><span class="line"><span class="selector-tag">video</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: auto;</span><br><span class="line">  <span class="attribute">max-width</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">iframe</span> &#123;</span><br><span class="line">  <span class="attribute">border</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">table</span> &#123;</span><br><span class="line">  <span class="attribute">border-collapse</span>: collapse;</span><br><span class="line">  <span class="attribute">border-spacing</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">td</span>,</span><br><span class="line"><span class="selector-tag">th</span> &#123;</span><br><span class="line">  <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>在 router 文件夹下新建<code>index.ts</code>文件</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createRouter, createWebHashHistory &#125; <span class="keyword">from</span> <span class="string">&quot;vue-router&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; RouteRecordRaw &#125; <span class="keyword">from</span> <span class="string">&quot;vue-router&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> Nprogress <span class="keyword">from</span> <span class="string">&quot;nprogress&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;nprogress/nprogress.css&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes: <span class="built_in">Array</span>&lt;RouteRecordRaw&gt; = [];</span><br><span class="line"><span class="comment">// import.meta.glob 为 vite 提供的特殊导入方式</span></span><br><span class="line"><span class="comment">// 它可以将模块中全部内容导入并返回一个Record对象</span></span><br><span class="line"><span class="comment">// 默认为懒加载模式 加入配置项 eager 取消懒加载</span></span><br><span class="line"><span class="keyword">const</span> modules: Record&lt;keyof <span class="built_in">any</span>, <span class="built_in">any</span>&gt; = <span class="keyword">import</span>.meta.glob([<span class="string">&#x27;./modules/*.ts&#x27;</span>], &#123;</span><br><span class="line">  eager: <span class="literal">true</span>,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">Object</span>.values(modules).forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">  routes.push(item.default);</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = createRouter(&#123;</span><br><span class="line">  history: createWebHashHistory(<span class="keyword">import</span>.meta.env.BASE_URL),</span><br><span class="line">  routes,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.beforeEach(<span class="keyword">async</span> (_to, _from, next) =&gt; &#123;</span><br><span class="line">  Nprogress.start();</span><br><span class="line">  next();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">router.afterEach(<span class="function">(<span class="params">_to</span>) =&gt;</span> &#123;</span><br><span class="line">  Nprogress.done();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router;</span><br></pre></td></tr></table></figure></li>
<li><p>在router文件夹下新建<code>modules</code>文件夹，并创建相关路由模块，例如</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// modules/login.ts</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  path: <span class="string">&#x27;/login&#x27;</span>,</span><br><span class="line">  name: <span class="string">&#x27;Login&#x27;</span>,</span><br><span class="line">  component: <span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;@/views/login/index.vue&#x27;</span>),</span><br><span class="line">  meta: &#123;&#125;,</span><br><span class="line">  children: [],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>在 store 文件夹下新建<code>index.ts</code>文件</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createPinia &#125; <span class="keyword">from</span> <span class="string">&quot;pinia&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> piniaPluginPersistedstate <span class="keyword">from</span> <span class="string">&quot;pinia-plugin-persistedstate&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> pinia = createPinia();</span><br><span class="line"><span class="comment">// 使用pinia数据持久化插件</span></span><br><span class="line">pinia.use(piniaPluginPersistedstate);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> pinia;</span><br></pre></td></tr></table></figure></li>
<li><p>在store文件夹下新建相关模块文件夹，例如user</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// store/user/types.ts</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> UserType = &#123;</span><br><span class="line">  username: <span class="built_in">string</span>;</span><br><span class="line">  accessToken: <span class="built_in">string</span>;</span><br><span class="line">  refreshToken?: <span class="built_in">string</span>;</span><br><span class="line">  roles: <span class="built_in">Array</span>&lt;<span class="built_in">string</span>&gt;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// store/user/index.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; defineStore &#125; <span class="keyword">from</span> <span class="string">&quot;pinia&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; <span class="keyword">from</span> <span class="string">&quot;vue&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; UserType &#125; <span class="keyword">from</span> <span class="string">&quot;./types&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; userLogin, refreshUserInfo &#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/user&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; LoginToInfo &#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/user/types&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> useUserStore = defineStore(<span class="string">&#x27;userInfo&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> userInfo = reactive&lt;UserType&gt;(&#123;</span><br><span class="line">    username: <span class="string">&#x27;王博文&#x27;</span>,</span><br><span class="line">    accessToken: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    roles: [<span class="string">&#x27;admin&#x27;</span>]</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> storeUserLogin = <span class="keyword">async</span> (data: LoginToInfo) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> res = <span class="keyword">await</span> userLogin(data);</span><br><span class="line">    userInfo.username = res.username;</span><br><span class="line">    userInfo.accessToken = res.accessToken;</span><br><span class="line">    userInfo.roles = res.roles;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> storeRefreshUserInfo = <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (userInfo.username === <span class="string">&#x27;王博文&#x27;</span> &amp;&amp; userInfo.accessToken) &#123;</span><br><span class="line">      <span class="keyword">const</span> res = <span class="keyword">await</span> refreshUserInfo(&#123;</span><br><span class="line">        accessToken: userInfo.accessToken</span><br><span class="line">      &#125;)</span><br><span class="line">      userInfo.username = res.username;</span><br><span class="line">      userInfo.accessToken = res.accessToken;</span><br><span class="line">      userInfo.roles = res.roles;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    storeUserLogin,</span><br><span class="line">    storeRefreshUserInfo</span><br><span class="line">  &#125;</span><br><span class="line">&#125;, &#123;</span><br><span class="line">  <span class="comment">// 这里是pinia持久化插件配置</span></span><br><span class="line">  persist: &#123;</span><br><span class="line">    key: <span class="string">&#x27;userInfo&#x27;</span>,</span><br><span class="line">    storage: sessionStorage,</span><br><span class="line">    paths: [<span class="string">&#x27;accessToken&#x27;</span>]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li><p>在 api 文件夹下新建<code>http.ts</code>文件</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Axios <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123;</span><br><span class="line">  AxiosError,</span><br><span class="line">  Canceler,</span><br><span class="line">  AxiosRequestConfig,</span><br><span class="line">  AxiosInstance,</span><br><span class="line">  AxiosResponse,</span><br><span class="line">  InternalAxiosRequestConfig</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;axios&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; ElMessage &#125; <span class="keyword">from</span> <span class="string">&#x27;element-plus&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> hasInternalAxiosRequestConfig = InternalAxiosRequestConfig &amp; &#123;</span><br><span class="line">  requestKey?: <span class="built_in">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> pendingRequest = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"><span class="keyword">const</span> service: AxiosInstance = Axios.create(&#123;</span><br><span class="line">  baseURL: <span class="keyword">import</span>.meta.env.VITE_APP_API_BASEURL,</span><br><span class="line">  timeout: <span class="number">15000</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 请求拦截器</span></span><br><span class="line">service.interceptors.request.use(</span><br><span class="line">  (config: hasInternalAxiosRequestConfig) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> requestKey = <span class="string">`<span class="subst">$&#123;config.url&#125;</span>&amp;request_type=<span class="subst">$&#123;config.method&#125;</span>`</span>;</span><br><span class="line">    <span class="keyword">if</span> (pendingRequest.has(requestKey)) &#123;</span><br><span class="line">      config.cancelToken = <span class="keyword">new</span> Axios.CancelToken(<span class="function">(<span class="params">cancel: Canceler</span>) =&gt;</span> &#123;</span><br><span class="line">        cancel(<span class="string">`重复请求已被拦截：<span class="subst">$&#123;requestKey&#125;</span>`</span>)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      pendingRequest.set(requestKey, config);</span><br><span class="line">      config.requestKey = requestKey;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> config;</span><br><span class="line">  &#125;,</span><br><span class="line">  (error: AxiosError) =&gt; &#123;</span><br><span class="line">    pendingRequest.clear();</span><br><span class="line">    ElMessage(&#123;</span><br><span class="line">      message: error.message || <span class="string">&#x27;请检查网络！&#x27;</span>,</span><br><span class="line">      <span class="keyword">type</span>: <span class="string">&#x27;error&#x27;</span>,</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error.response?.data);</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 响应拦截器</span></span><br><span class="line">service.interceptors.response.use(</span><br><span class="line">  (response: AxiosResponse &amp; &#123;</span><br><span class="line">    config?: hasInternalAxiosRequestConfig</span><br><span class="line">  &#125;) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; status, data &#125; = response;</span><br><span class="line">    <span class="keyword">const</span> key = response.config.requestKey;</span><br><span class="line">    pendingRequest.delete(key); <span class="comment">// 到响应拦截了说明已经接口成功</span></span><br><span class="line">    <span class="keyword">if</span> (status === <span class="number">200</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(<span class="keyword">new</span> <span class="built_in">Error</span>(data));</span><br><span class="line">  &#125;,</span><br><span class="line">  (error: AxiosError) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> msg = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> status = error.response?.status;</span><br><span class="line">    <span class="keyword">const</span> err = error.response?.data;</span><br><span class="line">    <span class="keyword">switch</span>(status) &#123;</span><br><span class="line">      <span class="keyword">case</span> <span class="number">400</span>:</span><br><span class="line">      msg = <span class="string">&quot;请求错误(400)&quot;</span>;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">403</span>:</span><br><span class="line">      msg = <span class="string">&quot;拒绝访问(403)&quot;</span>;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">401</span>:</span><br><span class="line">      msg = <span class="string">&quot;未授权(401)&quot;</span>;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">500</span>:</span><br><span class="line">      msg = <span class="string">&quot;服务器错误(500)&quot;</span>;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">503</span>:</span><br><span class="line">      msg = <span class="string">&quot;服务不可用(503)&quot;</span>;</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      msg = <span class="string">`连接出错(<span class="subst">$&#123;status&#125;</span>)!`</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ElMessage(&#123;</span><br><span class="line">      message: msg,</span><br><span class="line">      <span class="keyword">type</span>: <span class="string">&#x27;error&#x27;</span>,</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">if</span> (Axios.isCancel(error)) &#123;</span><br><span class="line">      ElMessage(&#123;</span><br><span class="line">        message: error.message,</span><br><span class="line">        <span class="keyword">type</span>: <span class="string">&#x27;error&#x27;</span>,</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err);</span><br><span class="line">    &#125;</span><br><span class="line">    pendingRequest.clear();</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err);</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* url: 接口地址</span></span><br><span class="line"><span class="comment">* data：接口所需的入参</span></span><br><span class="line"><span class="comment">* config：接口所需的配置，例如配置header</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> http = &#123;</span><br><span class="line">  get&lt;T = <span class="built_in">any</span>&gt;(url: <span class="built_in">string</span>, config?: AxiosRequestConfig): <span class="built_in">Promise</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> service.get(url, config);</span><br><span class="line">  &#125;,</span><br><span class="line">  post&lt;T = <span class="built_in">any</span>&gt;(url: <span class="built_in">string</span>, data?: Record&lt;keyof <span class="built_in">any</span>, <span class="built_in">any</span>&gt;, config?: AxiosRequestConfig): <span class="built_in">Promise</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> service.post(url, data, config);</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="keyword">delete</span>&lt;T = <span class="built_in">any</span>&gt;(url: <span class="built_in">string</span>, config?: AxiosRequestConfig): <span class="built_in">Promise</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> service.delete(url, config);</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 清空pendingRequests</span></span><br><span class="line">  <span class="function"><span class="title">clearRequestList</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    pendingRequest.clear();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="安装-vite-相关依赖"><a href="#安装-vite-相关依赖" class="headerlink" title="安装 vite 相关依赖"></a>安装 vite 相关依赖</h4></li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm install -D vite @vitejs/plugin-vue @vitejs/plugin-vue-jsx</span><br><span class="line"></span><br><span class="line">npm install -D @types/node @types/nprogress vue-tsc</span><br></pre></td></tr></table></figure>
<ul>
<li>vite：项目构建工具</li>
<li>@vitejs/plugin-vue：使 vite 能够编译 vue 组件</li>
<li>@vitejs/plugin-vue-jsx：使 vite 能够编译 jsx 组件</li>
<li>@types/node：node 类型包，使 ts 支持 node</li>
<li>@types/nprogress：nprogress 的类型支持</li>
<li>vue-tsc：vue 文件的类型检查工具</li>
</ul>
<h4 id="配置-vite-环境变量"><a href="#配置-vite-环境变量" class="headerlink" title="配置 vite 环境变量"></a>配置 vite 环境变量</h4><p>Vite 在一个特殊的<code>import.meta.env</code>对象上暴露环境变量。有些在所有情况下都可以使用的内建变量：</p>
<ul>
<li><code>import.meta.env.MODE</code>: {string}应用运行的模式</li>
<li><code>import.meta.env.BASE_URL</code>：{string}部署应用时的基本 URL</li>
<li><code>import.meta.env.PROD</code>：{boolean}应用是否在生产环境(使用<code>NODE_ENV=&#39;production&#39;</code>运行开发服务器或构建应用时使用)</li>
<li><code>import.meta.env.DEV</code>：{boolean}应用是否在开发环境(与 PROD 相反)</li>
<li><code>import.meta.env.SSR</code>：{boolean}应用是否运行在 server 上</li>
</ul>
<p>在目录的根目录下新建三个文件：</p>
<ul>
<li><code>.env</code>：所有情况下都会加载</li>
<li><code>.env.production</code>：npm run build 会加载</li>
<li><code>.env.development</code>：npm run dev 会加载</li>
</ul>
<p><code>.env.development</code>文件下配置 baseURL</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#axios 请求的baseURL</span><br><span class="line">VITE_APP_BASEURL = /api</span><br></pre></td></tr></table></figure>
<p>将 http.ts 文件中 axios 的 baseURL 替换</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> service: AxiosInstance = Axios.create(&#123;</span><br><span class="line">  baseURL: <span class="keyword">import</span>.meta.env.VITE_APP_BASEURL,</span><br><span class="line">  timeout: <span class="number">15000</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>配置 typescript 智能提示</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/// &lt;reference types=&quot;vite/client&quot; /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> ImportMetaEnv &#123;</span><br><span class="line">  <span class="keyword">readonly</span> VITE_APP_BASEURL: <span class="built_in">string</span>;</span><br><span class="line">  <span class="comment">// 更多环境变量...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> ImportMeta &#123;</span><br><span class="line">  <span class="keyword">readonly</span> env: ImportMetaEnv;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="配置-vite-config-ts"><a href="#配置-vite-config-ts" class="headerlink" title="配置 vite.config.ts"></a>配置 vite.config.ts</h4><p>在项目根目录下新建 vite.config.ts</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; defineConfig, loadEnv &#125; <span class="keyword">from</span> <span class="string">&quot;vite&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; UserConfig, ConfigEnv &#125; <span class="keyword">from</span> <span class="string">&quot;vite&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; fileURLToPath &#125; <span class="keyword">from</span> <span class="string">&quot;url&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> vue <span class="keyword">from</span> <span class="string">&quot;@vitejs/plugin-vue&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> vueJsx <span class="keyword">from</span> <span class="string">&quot;@vitejs/plugin-vue-jsx&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> defineConfig((&#123; mode &#125;: ConfigEnv): <span class="function"><span class="params">UserConfig</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 获取当前工作目录</span></span><br><span class="line">  <span class="keyword">const</span> root = process.cwd();</span><br><span class="line">  <span class="comment">// 获取环境变量</span></span><br><span class="line">  <span class="keyword">const</span> env = loadEnv(mode, root);</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="comment">// 项目根目录</span></span><br><span class="line">    root,</span><br><span class="line">    <span class="comment">// 项目部署的基础路径</span></span><br><span class="line">    base: <span class="string">&quot;/&quot;</span>,</span><br><span class="line">    publicDir: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&quot;./public&quot;</span>, <span class="keyword">import</span>.meta.url)), <span class="comment">// 无需处理的静态资源位置</span></span><br><span class="line">    assetsInclude: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&quot;./src/assets&quot;</span>, <span class="keyword">import</span>.meta.url)), <span class="comment">// 需要处理的静态资源位置</span></span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="comment">// Vue模板文件编译插件</span></span><br><span class="line">      vue(),</span><br><span class="line">      <span class="comment">// jsx文件编译插件</span></span><br><span class="line">      vueJsx(),</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 运行后本地预览的服务器</span></span><br><span class="line">    server: &#123;</span><br><span class="line">      <span class="comment">// 是否开启https</span></span><br><span class="line">      https: <span class="literal">false</span>,</span><br><span class="line">      <span class="comment">// 指定服务器应该监听哪个 IP 地址。 如果将此设置为 0.0.0.0 或者 true 将监听所有地址，包括局域网和公网地址。</span></span><br><span class="line">      host: <span class="literal">true</span>,</span><br><span class="line">      <span class="comment">// 开发环境预览服务器端口</span></span><br><span class="line">      port: <span class="number">3000</span>,</span><br><span class="line">      <span class="comment">// 启动后是否自动打开浏览器</span></span><br><span class="line">      open: <span class="literal">false</span>,</span><br><span class="line">      <span class="comment">// 是否开启CORS跨域</span></span><br><span class="line">      cors: <span class="literal">true</span>,</span><br><span class="line">      <span class="comment">// 代理服务器</span></span><br><span class="line">      <span class="comment">// 帮助我们开发时解决跨域问题</span></span><br><span class="line">      proxy: &#123;</span><br><span class="line">        <span class="comment">// 这里的意思是 以/api开头发送的请求都会被转发到 http://xxx:3000</span></span><br><span class="line">        <span class="string">&quot;/api&quot;</span>: &#123;</span><br><span class="line">          target: <span class="string">&quot;http://xxx:9000&quot;</span>,</span><br><span class="line">          <span class="comment">// 改变 Host Header</span></span><br><span class="line">          changeOrigin: <span class="literal">true</span>,</span><br><span class="line">          <span class="comment">// 发起请求时将 &#x27;/api&#x27; 替换为 &#x27;&#x27;</span></span><br><span class="line">          rewrite: <span class="function">(<span class="params">path</span>) =&gt;</span> path.replace(<span class="regexp">/^\/api/</span>, <span class="string">&quot;&quot;</span>),</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 打包配置</span></span><br><span class="line">    build: &#123;</span><br><span class="line">      <span class="comment">// 关闭 sorcemap 报错不会映射到源码</span></span><br><span class="line">      sourcemap: <span class="literal">false</span>,</span><br><span class="line">      <span class="comment">// 打包大小超出 400kb 提示警告</span></span><br><span class="line">      chunkSizeWarningLimit: <span class="number">400</span>,</span><br><span class="line">      rollupOptions: &#123;</span><br><span class="line">        <span class="comment">// 打包入口文件 根目录下的 index.html</span></span><br><span class="line">        <span class="comment">// 也就是项目从哪个文件开始打包</span></span><br><span class="line">        input: &#123;</span><br><span class="line">          index: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&quot;./index.html&quot;</span>, <span class="keyword">import</span>.meta.url)),</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 静态资源分类打包</span></span><br><span class="line">        output: &#123;</span><br><span class="line">          format: <span class="string">&quot;esm&quot;</span>,</span><br><span class="line">          chunkFileNames: <span class="string">&quot;static/js/[name]-[hash].js&quot;</span>,</span><br><span class="line">          entryFileNames: <span class="string">&quot;static/js/[name]-[hash].js&quot;</span>,</span><br><span class="line">          assetFileNames: <span class="string">&quot;static/[ext]/[name]-[hash].[ext]&quot;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置别名</span></span><br><span class="line">    resolve: &#123;</span><br><span class="line">      alias: &#123;</span><br><span class="line">        <span class="string">&quot;@&quot;</span>: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&quot;./src&quot;</span>, <span class="keyword">import</span>.meta.url)),</span><br><span class="line">        <span class="string">&quot;#&quot;</span>: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&quot;./types&quot;</span>, <span class="keyword">import</span>.meta.url)),</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="ts-配置文件"><a href="#ts-配置文件" class="headerlink" title="ts 配置文件"></a>ts 配置文件</h4><p>项目根目录下新建 tsconfig.json</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">    <span class="comment">// 编译出JS的目标ES版本</span></span><br><span class="line">    <span class="attr">&quot;target&quot;</span>: <span class="string">&quot;esnext&quot;</span>,</span><br><span class="line">    <span class="comment">// 使用的ES版本</span></span><br><span class="line">    <span class="attr">&quot;module&quot;</span>: <span class="string">&quot;esnext&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;allowJs&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="comment">// 用于选择模块解析策略，有&#x27;node&#x27;和&#x27;classic&#x27;两种类型</span></span><br><span class="line">    <span class="attr">&quot;moduleResolution&quot;</span>: <span class="string">&quot;node&quot;</span>,</span><br><span class="line">    <span class="comment">// 开启严格模式</span></span><br><span class="line">    <span class="attr">&quot;strict&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 强制代码中使用的模块文件名必须和文件系统中的文件名保持大小写一致</span></span><br><span class="line">    <span class="attr">&quot;forceConsistentCasingInFileNames&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 允许使用 xxx 代替 * as xxx 导入</span></span><br><span class="line">    <span class="attr">&quot;allowSyntheticDefaultImports&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 指定 jsx 代码的生成: &#x27;preserve&#x27;, &#x27;react-native&#x27;, or &#x27;react&#x27;</span></span><br><span class="line">    <span class="attr">&quot;jsx&quot;</span>: <span class="string">&quot;preserve&quot;</span>,</span><br><span class="line">    <span class="comment">// 用来指定编译时是否生成.map文件</span></span><br><span class="line">    <span class="attr">&quot;sourceMap&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 通过为导入内容创建命名空间，实现CommonJS和ES模块之间的互操作性</span></span><br><span class="line">    <span class="attr">&quot;esModuleInterop&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 是否可以导入 json module</span></span><br><span class="line">    <span class="attr">&quot;resolveJsonModule&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 是否检测定义了但是没使用的变量</span></span><br><span class="line">    <span class="attr">&quot;noUnusedLocals&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 是否检查是否有在函数体中没有使用的参数</span></span><br><span class="line">    <span class="attr">&quot;noUnusedParameters&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 是否启用实验性的装饰器特性</span></span><br><span class="line">    <span class="attr">&quot;experimentalDecorators&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// ts中可以使用的库 这里配置为 dom 与 es模块</span></span><br><span class="line">    <span class="attr">&quot;lib&quot;</span>: [<span class="string">&quot;dom&quot;</span>, <span class="string">&quot;esnext&quot;</span>],</span><br><span class="line">    <span class="comment">// 不允许变量或函数参数具有隐式any类型</span></span><br><span class="line">    <span class="attr">&quot;noImplicitAny&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="comment">// 启用阻止对下载库的类型检查</span></span><br><span class="line">    <span class="attr">&quot;skipLibCheck&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// types用来指定需要包含的模块</span></span><br><span class="line">    <span class="attr">&quot;types&quot;</span>: [<span class="string">&quot;vite/client&quot;</span>, <span class="string">&quot;element-plus/global&quot;</span>],</span><br><span class="line">    <span class="comment">// 编译的时候删除注释</span></span><br><span class="line">    <span class="attr">&quot;removeComments&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 使用绝对路径时使用baseUrl去解析导入路径</span></span><br><span class="line">    <span class="attr">&quot;baseUrl&quot;</span>: <span class="string">&quot;.&quot;</span>,</span><br><span class="line">    <span class="comment">// 为导入路径配置别名</span></span><br><span class="line">    <span class="attr">&quot;paths&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;@/*&quot;</span>: [<span class="string">&quot;src/*&quot;</span>],</span><br><span class="line">      <span class="attr">&quot;#/*&quot;</span>: [<span class="string">&quot;types/*&quot;</span>]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置虚拟目录</span></span><br><span class="line">    <span class="attr">&quot;rootDirs&quot;</span>: []</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 指定需要编译文件</span></span><br><span class="line">  <span class="attr">&quot;include&quot;</span>: [</span><br><span class="line">    <span class="string">&quot;src/**/*.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;src/**/*.d.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;src/**/*.tsx&quot;</span>,</span><br><span class="line">    <span class="string">&quot;src/**/*.vue&quot;</span>,</span><br><span class="line">    <span class="string">&quot;types/**/*.d.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;types/**/*.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;build/**/*.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;build/**/*.d.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;mock/**/*.ts&quot;</span>,</span><br><span class="line">    <span class="string">&quot;vite.config.ts&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="comment">// 指定不需要编译的文件</span></span><br><span class="line">  <span class="attr">&quot;exclude&quot;</span>: [<span class="string">&quot;node_modules&quot;</span>, <span class="string">&quot;dist&quot;</span>, <span class="string">&quot;**/*.js&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="package-json下配置命令"><a href="#package-json下配置命令" class="headerlink" title="package.json下配置命令"></a>package.json下配置命令</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&quot;scripts&quot;: &#123;</span><br><span class="line">  &quot;dev&quot;: &quot;vite --mode development&quot;,</span><br><span class="line">  &quot;build&quot;: &quot;vite build --mode production&quot;,</span><br><span class="line">  &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="配置ESLint"><a href="#配置ESLint" class="headerlink" title="配置ESLint"></a>配置ESLint</h4><p>ESLint是一个用来识别ecmascript并且按照规则给出报告的代码检测工具，避免低级错误和统一的代码风格：</p>
<ul>
<li><p>查出JavaScript的语法问题</p>
</li>
<li><p>根据配置的规则，标记不符合规范的代码</p>
</li>
<li><p>自动修复一些结构，风格问题</p>
</li>
<li><p>官方文档<br>配置规则：<a target="_blank" rel="noopener" href="https://eslint.cn/">ESLint中文网</a><br>rules配置：(<a target="_blank" rel="noopener" href="https://eslint.cn/docs/rules/">https://eslint.cn/docs/rules/</a>)</p>
</li>
<li><p>安装依赖<br>@eslint/create-config：eslint配置文件初始化工具</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D eslint @eslint/<span class="built_in">create</span>-<span class="built_in">config</span></span><br></pre></td></tr></table></figure></li>
<li><p>ESLint扩展安装<br>在vscode的扩展面板中直接搜索<code>eslint</code>，然后安装第一个</p>
</li>
<li><p>自动保存修复</p>
<!-- 每次修改完代码，可以在保存的时候对代码进行修复，依次点击`文件 => 首选项 => 设置`,打开`settings.json`文件
![Alt text](image-5.png)
然后将以下代码添加进去，保存后重启编辑器 -->
<p><img src="image-6.png" alt="Alt text"><img src="image-7.png" alt="Alt text"><br>通过上述图，在工作台生成一个setting.json文件，并添加如下配置：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&quot;editor.codeActionsOnSave&quot;: &#123;</span><br><span class="line">  &quot;source.fixAll&quot;: true,</span><br><span class="line">  &quot;source.fixAll.eslint&quot;: true</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 在less写样式保存后，会自动生成一个css文件，下面配置是解决该问题的</span></span><br><span class="line">&quot;less.compile&quot;: &#123;</span><br><span class="line">  &quot;compress&quot;: false,//是否压缩</span><br><span class="line">  &quot;sourceMap&quot;: false,//是否生成map文件，有了这个可以在调试太看到less行数</span><br><span class="line">  &quot;out&quot;: false,//是否输出为css文件，false不输出</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></li>
<li><p>生成配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npx eslint --init</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 出现如下选择</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择2 我们会使用 prettier 进行代码风格校验</span></span><br><span class="line">How would you like to use ESLint?</span><br><span class="line">1.只检查语法</span><br><span class="line">2.检查语法并提示问题</span><br><span class="line">3.检查语法、提示问题并且强制使用一些代码风格</span><br><span class="line"></span><br><span class="line"><span class="comment"># 你的项目用的哪一种模块化方式 选择1</span></span><br><span class="line">What <span class="built_in">type</span> of modules does your project use?</span><br><span class="line">1.ES6</span><br><span class="line">2.CommonJS</span><br><span class="line">3.None</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用的框架 选择2</span></span><br><span class="line">Which framework does your project use?</span><br><span class="line">1.React</span><br><span class="line">2.Vue.js</span><br><span class="line">3.None</span><br><span class="line"></span><br><span class="line"><span class="comment"># 项目是否使用TS yes</span></span><br><span class="line">Does your project use TypeScript?</span><br><span class="line"></span><br><span class="line"><span class="comment"># 项目在哪里跑的 选择1</span></span><br><span class="line">Where does your code run?</span><br><span class="line">1.browser</span><br><span class="line">2.node</span><br><span class="line"></span><br><span class="line"><span class="comment"># 项目用哪种配置文件 选择1</span></span><br><span class="line">What format <span class="keyword">do</span> you want your config file to be <span class="keyword">in</span>?</span><br><span class="line">1.JavaScript</span><br><span class="line">2.YAML</span><br><span class="line">3.JSON</span><br><span class="line"></span><br><span class="line"><span class="comment"># 是否立即安装需要的依赖</span></span><br><span class="line">Would you like to install them now?</span><br><span class="line"><span class="comment"># 会帮我们安装如下插件</span></span><br><span class="line"><span class="comment"># pnpm install -D eslint-plugin-vue@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest</span></span><br></pre></td></tr></table></figure>
<p>  <img src="image-4.png" alt="Alt text"></p>
</li>
<li><p>ESLint配置文件.eslintrc.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  env: &#123;</span><br><span class="line">    browser: <span class="literal">true</span>,</span><br><span class="line">    es2021: <span class="literal">true</span>,</span><br><span class="line">    node: <span class="literal">true</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="keyword">extends</span>: [</span><br><span class="line">    <span class="string">&#x27;eslint:recommended&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;plugin:@typescript-eslint/recommended&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;plugin:vue/vue3-essential&#x27;</span></span><br><span class="line">  ],</span><br><span class="line">  overrides: [</span><br><span class="line">    &#123;</span><br><span class="line">      env: &#123;</span><br><span class="line">        node: <span class="literal">true</span></span><br><span class="line">      &#125;,</span><br><span class="line">      files: [<span class="string">&#x27;.eslintrc.&#123;js,cjs&#125;&#x27;</span>],</span><br><span class="line">      parserOptions: &#123;</span><br><span class="line">        sourceType: <span class="string">&#x27;script&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="comment">// 选择使用的解析器</span></span><br><span class="line">  parser: <span class="string">&#x27;vue-eslint-parser&#x27;</span>,</span><br><span class="line">  parserOptions: &#123;</span><br><span class="line">    ecmaVersion: <span class="string">&#x27;latest&#x27;</span>,</span><br><span class="line">    parser: <span class="string">&#x27;@typescript-eslint/parser&#x27;</span>,</span><br><span class="line">    sourceType: <span class="string">&#x27;module&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  plugins: [<span class="string">&#x27;@typescript-eslint&#x27;</span>, <span class="string">&#x27;vue&#x27;</span>],</span><br><span class="line">  <span class="comment">/*</span></span><br><span class="line"><span class="comment">  * &quot;off&quot; 或 0    ==&gt;  关闭规则</span></span><br><span class="line"><span class="comment">  * &quot;warn&quot; 或 1   ==&gt;  打开的规则作为警告（不影响代码执行）</span></span><br><span class="line"><span class="comment">  * &quot;error&quot; 或 2  ==&gt;  规则作为一个错误（代码不能执行，界面报错）</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  rules: &#123;</span><br><span class="line">    <span class="comment">// eslint (http://eslint.cn/docs/rules)</span></span><br><span class="line">    <span class="string">&quot;no-var&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 要求使用 let 或 const 而不是 var</span></span><br><span class="line">    <span class="string">&quot;no-multiple-empty-lines&quot;</span>: [<span class="string">&quot;error&quot;</span>, &#123; <span class="attr">max</span>: <span class="number">1</span> &#125;], <span class="comment">// 不允许多个空行</span></span><br><span class="line">    <span class="string">&quot;no-use-before-define&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止在 函数/类/变量 定义之前使用它们</span></span><br><span class="line">    <span class="string">&quot;prefer-const&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 此规则旨在标记使用 let 关键字声明但在初始分配后从未重新分配的变量，要求使用 const</span></span><br><span class="line">    <span class="string">&quot;no-irregular-whitespace&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止不规则的空白</span></span><br><span class="line">    <span class="string">&quot;max-len&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 强制每行代码字符数不超过指定长度</span></span><br><span class="line">    eqeqeq: <span class="string">&quot;error&quot;</span>, <span class="comment">// 只允许使用=== 和 !==</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// typeScript (https://typescript-eslint.io/rules)</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-unused-vars&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 禁止定义未使用的变量</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/prefer-ts-expect-error&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 禁止使用 @ts-ignore</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-inferrable-types&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 可以轻松推断的显式类型可能会增加不必要的冗长</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-namespace&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止使用自定义 TypeScript 模块和命名空间。</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-explicit-any&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止使用 any 类型</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/ban-types&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止使用特定类型</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/explicit-function-return-type&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 不允许对初始化为数字、字符串或布尔值的变量或参数进行显式类型声明</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-var-requires&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 不允许在 import 语句中使用 require 语句</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-empty-function&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止空函数</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-use-before-define&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止在变量定义之前使用它们</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/ban-ts-comment&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止 @ts-&lt;directive&gt; 使用注释或要求在指令后进行描述</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/no-non-null-assertion&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 不允许使用后缀运算符的非空断言(!)</span></span><br><span class="line">    <span class="string">&quot;@typescript-eslint/explicit-module-boundary-types&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 要求导出函数和类的公共类方法的显式返回和参数类型</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// vue (https://eslint.vuejs.org/rules)</span></span><br><span class="line">    <span class="string">&quot;vue/no-v-html&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 禁止使用 v-html</span></span><br><span class="line">    <span class="string">&quot;vue/script-setup-uses-vars&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 防止&lt;script setup&gt;使用的变量&lt;template&gt;被标记为未使用，此规则仅在启用该no-unused-vars规则时有效。</span></span><br><span class="line">    <span class="string">&quot;vue/v-slot-style&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 强制执行 v-slot 指令样式</span></span><br><span class="line">    <span class="string">&quot;vue/no-mutating-props&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 不允许组件 prop的改变</span></span><br><span class="line">    <span class="string">&quot;vue/custom-event-name-casing&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 为自定义事件名称强制使用特定大小写</span></span><br><span class="line">    <span class="string">&quot;vue/attributes-order&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// vue api使用顺序，强制执行属性顺序</span></span><br><span class="line">    <span class="string">&quot;vue/one-component-per-file&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 强制每个组件都应该在自己的文件中</span></span><br><span class="line">    <span class="string">&quot;vue/html-closing-bracket-newline&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 在标签的右括号之前要求或禁止换行</span></span><br><span class="line">    <span class="string">&quot;vue/max-attributes-per-line&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 强制每行的最大属性数</span></span><br><span class="line">    <span class="string">&quot;vue/multiline-html-element-content-newline&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 在多行元素的内容之前和之后需要换行符</span></span><br><span class="line">    <span class="string">&quot;vue/singleline-html-element-content-newline&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 在单行元素的内容之前和之后需要换行符</span></span><br><span class="line">    <span class="string">&quot;vue/attribute-hyphenation&quot;</span>: <span class="string">&quot;off&quot;</span>, <span class="comment">// 对模板中的自定义组件强制执行属性命名样式</span></span><br><span class="line">    <span class="string">&quot;vue/require-default-prop&quot;</span>: <span class="string">&quot;error&quot;</span>, <span class="comment">// 此规则要求为每个 prop 为必填时，必须提供默认值</span></span><br><span class="line">    <span class="string">&quot;vue/multi-word-component-names&quot;</span>: <span class="string">&quot;off&quot;</span> <span class="comment">// 要求组件名称始终为 “-” 链接的单词</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>配置完后在在package.json里添加一条指令</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// --cache 为仅检测改动过的代码</span></span><br><span class="line"><span class="comment">// --max-warnings 0 表示出现超过0个警告强制eslint以错误状态推出</span></span><br><span class="line">&quot;scripts&quot;: &#123;</span><br><span class="line">  &quot;lint:eslint&quot;: &quot;eslint --cache --max-warnings 0 &#123;src,mock&#125;/**/*.&#123;vue,ts,tsx&#125; --fix&quot;,</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></li>
<li><p>新建忽略文件.eslintignore并配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">*.sh</span><br><span class="line">node_modules</span><br><span class="line">*.md</span><br><span class="line">*.woff</span><br><span class="line">*.ttf</span><br><span class="line">.vscode</span><br><span class="line">.idea</span><br><span class="line">dist</span><br><span class="line">/public</span><br><span class="line">/docs</span><br><span class="line">.husky</span><br><span class="line">.local</span><br><span class="line">/bin</span><br><span class="line">.eslintrc.js</span><br><span class="line">.prettierrc.js</span><br><span class="line">/src/mock/*</span><br></pre></td></tr></table></figure>
<h4 id="配置Prettier"><a href="#配置Prettier" class="headerlink" title="配置Prettier"></a>配置Prettier</h4></li>
</ul>
<p>Prettier可以帮助项目开发时，保持统一的代码风格，例如是否使用分号，缩进的格式等等</p>
<ul>
<li><p>官方文档<br><a target="_blank" rel="noopener" href="https://www.prettier.cn/">Prettier中文文档</a></p>
</li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D prettier</span><br></pre></td></tr></table></figure></li>
<li><p>添加配置文件<br>配置文件名称可以为：.prettierrc.js、.prettier.config.js</p>
</li>
<li><p>常用配置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="comment">// 超过最大值换行</span></span><br><span class="line">  printWidth: <span class="number">130</span>,</span><br><span class="line">  <span class="comment">// 缩进字节数</span></span><br><span class="line">  tabWidth: <span class="number">2</span>,</span><br><span class="line">  <span class="comment">// 使用制表符而不是空格缩进行</span></span><br><span class="line">  useTabs: <span class="literal">true</span>,</span><br><span class="line">  <span class="comment">// 结尾不用分号(true有，false没有)</span></span><br><span class="line">  semi: <span class="literal">true</span>,</span><br><span class="line">  <span class="comment">// 使用单引号(true单双引号，false双引号)</span></span><br><span class="line">  singleQuote: <span class="literal">false</span>,</span><br><span class="line">  <span class="comment">// 更改引用对象属性的时间 可选值&quot;&lt;as-needed|consistent|preserve&gt;&quot;</span></span><br><span class="line">  quoteProps: <span class="string">&quot;as-needed&quot;</span>,</span><br><span class="line">  <span class="comment">// 在对象，数组括号与文字之间加空格 &quot;&#123; foo: bar &#125;&quot;</span></span><br><span class="line">  bracketSpacing: <span class="literal">true</span>,</span><br><span class="line">  <span class="comment">// 多行时尽可能打印尾随逗号。（例如，单行数组永远不会出现逗号结尾。） 可选值&quot;&lt;none|es5|all&gt;&quot;，默认none</span></span><br><span class="line">  trailingComma: <span class="string">&quot;none&quot;</span>,</span><br><span class="line">  <span class="comment">// 在JSX中使用单引号而不是双引号</span></span><br><span class="line">  jsxSingleQuote: <span class="literal">false</span>,</span><br><span class="line">  <span class="comment">//  (x) =&gt; &#123;&#125; 箭头函数参数只有一个时是否要有小括号。avoid：省略括号 ,always：不省略括号</span></span><br><span class="line">  arrowParens: <span class="string">&quot;avoid&quot;</span>,</span><br><span class="line">  <span class="comment">// 如果文件顶部已经有一个 doclock，这个选项将新建一行注释，并打上@format标记。</span></span><br><span class="line">  insertPragma: <span class="literal">false</span>,</span><br><span class="line">  <span class="comment">// 指定要使用的解析器，不需要写文件开头的 @prettier</span></span><br><span class="line">  requirePragma: <span class="literal">false</span>,</span><br><span class="line">  <span class="comment">// 默认值。因为使用了一些折行敏感型的渲染器（如GitHub comment）而按照markdown文本样式进行折行</span></span><br><span class="line">  proseWrap: <span class="string">&quot;preserve&quot;</span>,</span><br><span class="line">  <span class="comment">// 在html中空格是否是敏感的 &quot;css&quot; - 遵守CSS显示属性的默认值， &quot;strict&quot; - 空格被认为是敏感的 ，&quot;ignore&quot; - 空格被认为是不敏感的</span></span><br><span class="line">  htmlWhitespaceSensitivity: <span class="string">&quot;css&quot;</span>,</span><br><span class="line">  <span class="comment">// 换行符使用 lf 结尾是 可选值&quot;&lt;auto|lf|crlf|cr&gt;&quot;</span></span><br><span class="line">  endOfLine: <span class="string">&quot;auto&quot;</span>,</span><br><span class="line">  <span class="comment">// 这两个选项可用于格式化以给定字符偏移量（分别包括和不包括）开始和结束的代码</span></span><br><span class="line">  rangeStart: <span class="number">0</span>,</span><br><span class="line">  rangeEnd: <span class="literal">Infinity</span>,</span><br><span class="line">  <span class="comment">// Vue文件脚本和样式标签缩进</span></span><br><span class="line">  vueIndentScriptAndStyle: <span class="literal">false</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>在package.json中配置一个指令便于使用prettier进行修复代码风格</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;lint:prettier&quot;</span>: <span class="string">&quot;prettier --write **/*.&#123;js,json,tsx,css,less,scss,vue,html,md&#125;&quot;</span>,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="解决ESLint和Prettier冲突"><a href="#解决ESLint和Prettier冲突" class="headerlink" title="解决ESLint和Prettier冲突"></a>解决ESLint和Prettier冲突</h4></li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D eslint-config-prettier eslint-plugin-prettier</span><br></pre></td></tr></table></figure>
<ul>
<li>eslint-config-prettier 的作用是关闭eslint中与prettier相互冲突的规则</li>
<li>eslint-plugin-prettier 的作用是赋予eslint用prettier格式化代码的能力</li>
</ul>
</li>
<li><p>配置ESlint</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&quot;extends&quot;: [</span><br><span class="line">  &quot;eslint:recommended&quot;,</span><br><span class="line">  &quot;plugin:vue/vue3-recommended&quot;,</span><br><span class="line">  &quot;plugin:@typescript-eslint/recommended&quot;,</span><br><span class="line">  &quot;prettier&quot;,</span><br><span class="line">  <span class="string">&quot;plugin:prettier/recommended&quot;</span> <span class="comment">// 在最后面新增extends</span></span><br><span class="line">],</span><br></pre></td></tr></table></figure></li>
<li><p>新建.prettierignore并配置无需校验的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">/dist/*</span><br><span class="line">.<span class="built_in">local</span></span><br><span class="line">/node_modules/**</span><br><span class="line"></span><br><span class="line">**/*.svg</span><br><span class="line">**/*.sh</span><br><span class="line"></span><br><span class="line">/public/*</span><br></pre></td></tr></table></figure>
<h4 id="配置StyleLint"><a href="#配置StyleLint" class="headerlink" title="配置StyleLint"></a>配置StyleLint</h4></li>
</ul>
<p>stylelint是一个强大的、先进的CSS代码检查器，可以帮助规避css代码中的错误并保持一致的编码风格</p>
<ul>
<li>安装依赖<!-- npm i -D stylelint-config-recommended stylelint-config-recommended-scss stylelint-config-recommended-vue postcss stylelint-config-recommended-less -->
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">npm install -D stylelint@14 stylelint-config-standard@25</span><br><span class="line"></span><br><span class="line">// 如果安装依赖时，控制台报错unable to resolve dependency tree，可以在最后加入**--legacy-peer-deps**</span><br><span class="line">// stylelint-config-prettier需指定版本为8.x，不然会和其他包文件冲突</span><br><span class="line">npm install -D stylelint-config-prettier@8 stylelint-config-html stylelint-order stylelint-less postcss-html postcss-less stylelint-config-standard-vue stylelint-config-standard-less stylelint-config-recommended-less stylelint-config-recommended-vue</span><br></pre></td></tr></table></figure>
<code>stylelint-config-standard</code>：StyleLint推荐配置<br><code>stylelint-config-prettier</code>：关闭与prettier冲突的配置<br><code>stylelint-config-standard-vue</code>：StyleLint Vue项目推荐配置<br><code>postcss-html postcss-less</code>：支持检查less 与 html<br><code>stylelint-order</code>：支持css样式排序<br><code>postcss</code>：postcss-html 的依赖包，可以用于转换 css 代码<br><code>stylelint-config-recommended-less</code>：推荐的less增强</li>
<li>添加配置文件<br>新建Prettier配置文件：.stylelintrc.js 或 stylelint.config.js<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  plugins: [<span class="string">&#x27;stylelint-order&#x27;</span>],</span><br><span class="line">  <span class="keyword">extends</span>: [</span><br><span class="line">    <span class="string">&quot;stylelint-config-standard&quot;</span>,</span><br><span class="line">    <span class="string">&quot;stylelint-config-prettier&quot;</span>,</span><br><span class="line">    <span class="string">&quot;stylelint-config-recommended-less&quot;</span>,</span><br><span class="line">    <span class="string">&quot;stylelint-config-standard-vue&quot;</span>,</span><br><span class="line">  ],</span><br><span class="line">  overrides: [</span><br><span class="line">    &#123;</span><br><span class="line">      files: [<span class="string">&#x27;**/*.&#123;htm,html,vue&#125;&#x27;</span>],</span><br><span class="line">      customSyntax: <span class="string">&#x27;postcss-html&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      files: [<span class="string">&#x27;**/*.&#123;css,less&#125;&#x27;</span>],</span><br><span class="line">      customSyntax: <span class="string">&#x27;postcss-less&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  rules: &#123;</span><br><span class="line">    <span class="comment">// 命名规范</span></span><br><span class="line">    <span class="string">&#x27;selector-class-pattern&#x27;</span>: [</span><br><span class="line">      <span class="string">&#x27;^([a-z][a-z0-9]*)(-[a-z0-9]+)*$&#x27;</span>,</span><br><span class="line">      &#123;</span><br><span class="line">        message: <span class="string">&#x27;Expected class selector to be kebab-case&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">    <span class="string">&#x27;string-quotes&#x27;</span>: <span class="string">&#x27;single&#x27;</span>, <span class="comment">// 单引号</span></span><br><span class="line">    <span class="string">&#x27;at-rule-empty-line-before&#x27;</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="string">&#x27;at-rule-no-unknown&#x27;</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="string">&#x27;at-rule-name-case&#x27;</span>: <span class="string">&#x27;lower&#x27;</span>, <span class="comment">// 指定@规则名的大小写</span></span><br><span class="line">    <span class="string">&#x27;length-zero-no-unit&#x27;</span>: <span class="literal">true</span>, <span class="comment">// 禁止零长度的单位（可自动修复）</span></span><br><span class="line">    <span class="string">&#x27;shorthand-property-no-redundant-values&#x27;</span>: <span class="literal">true</span>, <span class="comment">// 简写属性</span></span><br><span class="line">    <span class="string">&#x27;number-leading-zero&#x27;</span>: <span class="string">&#x27;never&#x27;</span>, <span class="comment">// 小数不带0</span></span><br><span class="line">    <span class="string">&#x27;declaration-block-no-duplicate-properties&#x27;</span>: <span class="literal">null</span>, <span class="comment">// 禁止声明快重复属性</span></span><br><span class="line">    <span class="string">&#x27;no-descending-specificity&#x27;</span>: <span class="literal">null</span>, <span class="comment">// 禁止在具有较高优先级的选择器后出现被其覆盖的较低优先级的选择器。</span></span><br><span class="line">    <span class="string">&#x27;selector-max-id&#x27;</span>: <span class="number">3</span>, <span class="comment">// 限制一个选择器中 ID 选择器的数量</span></span><br><span class="line">    <span class="string">&#x27;max-nesting-depth&#x27;</span>: <span class="number">4</span>,</span><br><span class="line">    indentation: [</span><br><span class="line">      <span class="number">2</span>,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="comment">// 指定缩进  warning 提醒</span></span><br><span class="line">        severity: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">    <span class="comment">// 规则顺序</span></span><br><span class="line">    <span class="string">&#x27;order/properties-order&#x27;</span>: [</span><br><span class="line">      <span class="string">&#x27;position&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;top&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;right&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;bottom&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;left&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;z-index&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;display&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;float&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;width&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;height&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;max-width&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;max-height&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;min-width&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;min-height&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;padding&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;padding-top&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;padding-right&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;padding-bottom&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;padding-left&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-top&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-right&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-bottom&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-left&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-collapse&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-top-collapse&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-right-collapse&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-bottom-collapse&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;margin-left-collapse&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;overflow&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;overflow-x&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;overflow-y&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;clip&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;clear&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font-family&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font-size&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font-smoothing&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;osx-font-smoothing&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font-style&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;font-weight&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;line-height&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;letter-spacing&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;word-spacing&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;color&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-align&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-decoration&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-indent&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-overflow&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-rendering&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-size-adjust&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-shadow&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;text-transform&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;word-break&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;word-wrap&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;white-space&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;vertical-align&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;list-style&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;list-style-type&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;list-style-position&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;list-style-image&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;pointer-events&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;cursor&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;background&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;background-color&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;border&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;border-radius&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;content&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;outline&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;outline-offset&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;opacity&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;filter&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;visibility&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;size&#x27;</span>,</span><br><span class="line">      <span class="string">&#x27;transform&#x27;</span></span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li>新建.stylelintignore配置忽略文件<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/dist/*</span><br><span class="line">/public/*</span><br><span class="line">public/*</span><br><span class="line">/mock/*</span><br><span class="line">/node_modules/*</span><br><span class="line">/plop-templates/*</span><br><span class="line">/types/*</span><br><span class="line"></span><br><span class="line"><span class="comment"># 其他忽略文件</span></span><br><span class="line">*.js</span><br><span class="line">*.jsx</span><br><span class="line">*.tsx</span><br><span class="line">*.ts</span><br><span class="line">*.json</span><br><span class="line">*.png</span><br><span class="line">*.eot</span><br><span class="line">*.ttf</span><br><span class="line">*.woff</span><br><span class="line">*.md</span><br></pre></td></tr></table></figure>
<h4 id="配置Husky"><a href="#配置Husky" class="headerlink" title="配置Husky"></a>配置Husky</h4></li>
</ul>
<p>Husky帮助我们在代码提交前后进行一些自定义操作，比如代码风格的校验，并且它支持所有的 Git 钩子（钩子是你可以放在钩子目录中触发的程序 在 Git 执行的某些点执行的操作）</p>
<p>Git全部钩子的详细介绍：<a target="_blank" rel="noopener" href="https://git-scm.com/docs/githooks">Git - githooks Documentation (git-scm.com)</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D husky</span><br></pre></td></tr></table></figure>
<p>配置一个初始化Husky命令</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;prepare&quot;</span>: <span class="string">&quot;husky install&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后运行这个命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run prepare</span><br></pre></td></tr></table></figure>
<p>运行后根目录就会生成.husky文件夹</p>
<h4 id="配置LintStaged"><a href="#配置LintStaged" class="headerlink" title="配置LintStaged"></a>配置LintStaged</h4><p>它可以帮助我们在 git 缓存中进行代码质量与风格的修复，在代码提交前进行统一校验，通过后才可以提交。</p>
<ul>
<li>安装依赖<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D lint-staged</span><br></pre></td></tr></table></figure></li>
<li>在package.json中配置指令<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;lint:lint-staged&quot;</span>: <span class="string">&quot;lint-staged&quot;</span>,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>使用husky配置一个 pre-commit 钩子<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># --no-install 代表强制使用本地模块</span></span><br><span class="line">npx husky add .husky/pre-commit <span class="string">&quot;npm run lint:lint-staged&quot;</span></span><br></pre></td></tr></table></figure></li>
<li>LintStaged可以在<code>package.json</code> 或 新建 <code>lint-staged.js</code>、<code>lint-staged.config.js</code>、<code>.lintstagedrc.json</code>、<code>.lintstagedrc.yaml</code></li>
<li>在package.json中配置<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&quot;lint-staged&quot;: &#123;</span><br><span class="line">  &quot;*.&#123;js,jsx,ts,tsx&#125;&quot;: [</span><br><span class="line">    &quot;eslint --fix&quot;,</span><br><span class="line">    <span class="string">&quot;prettier --write&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  &quot;&#123;!(package)*.json,*.code-snippets,.!(browserslist)*rc&#125;&quot;: [</span><br><span class="line">    <span class="string">&quot;prettier --write--parser json&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  &quot;package.json&quot;: [</span><br><span class="line">    <span class="string">&quot;prettier --write&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  &quot;*.vue&quot;: [</span><br><span class="line">    &quot;eslint --fix&quot;,</span><br><span class="line">    &quot;prettier --write&quot;,</span><br><span class="line">    <span class="string">&quot;stylelint --fix&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  &quot;*.&#123;scss,less,styl,html&#125;&quot;: [</span><br><span class="line">    &quot;stylelint --fix&quot;,</span><br><span class="line">    <span class="string">&quot;prettier --write&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  &quot;*.md&quot;: [</span><br><span class="line">    <span class="string">&quot;prettier --write&quot;</span></span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="配置CommitLint"><a href="#配置CommitLint" class="headerlink" title="配置CommitLint"></a>配置CommitLint</h4></li>
</ul>
<p>CommitLint对提交的信息进行规范，每次提交需要例如feat: xxx的格式</p>
<ul>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D @commitlint/cli @commitlint/config-conventional</span><br></pre></td></tr></table></figure>
<p>@commitlint/config-conventional：commitlint自定义配置规则插件</p>
</li>
<li><p>CommitLint配置文件名称可以是：.commitlintrc.js/.commitlint.config.js</p>
</li>
<li><p>配置文件内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 警告级别</span></span><br><span class="line"><span class="comment">// 0 无提示 1 警告 2错误</span></span><br><span class="line"><span class="comment">// 是否启用</span></span><br><span class="line"><span class="comment">// always 启用 never 禁用</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  ignores: [<span class="function">(<span class="params">commit</span>) =&gt;</span> commit.includes(<span class="string">&#x27;init&#x27;</span>)],</span><br><span class="line">  <span class="keyword">extends</span>: [<span class="string">&#x27;@commitlint/config-conventional&#x27;</span>],</span><br><span class="line">  rules: &#123;</span><br><span class="line">    <span class="comment">// 信息以空格开头</span></span><br><span class="line">    <span class="string">&#x27;body-leading-blank&#x27;</span>: [<span class="number">2</span>, <span class="string">&#x27;always&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;footer-leading-blank&#x27;</span>: [<span class="number">2</span>, <span class="string">&#x27;always&#x27;</span>],</span><br><span class="line">    <span class="comment">// 信息最大长度</span></span><br><span class="line">    <span class="string">&#x27;header-max-length&#x27;</span>: [<span class="number">2</span>, <span class="string">&#x27;always&#x27;</span>, <span class="number">108</span>],</span><br><span class="line">    <span class="comment">// 信息不能未空</span></span><br><span class="line">    <span class="string">&#x27;subject-empty&#x27;</span>: [<span class="number">2</span>, <span class="string">&#x27;never&#x27;</span>],</span><br><span class="line">    <span class="comment">// 信息类型不能未空</span></span><br><span class="line">    <span class="string">&#x27;type-empty&#x27;</span>: [<span class="number">2</span>, <span class="string">&#x27;never&#x27;</span>],</span><br><span class="line">    <span class="comment">// 提交信息的类型 下文有介绍</span></span><br><span class="line">    <span class="string">&#x27;type-enum&#x27;</span>: [</span><br><span class="line">      <span class="number">2</span>,</span><br><span class="line">      <span class="string">&#x27;always&#x27;</span>,</span><br><span class="line">      [</span><br><span class="line">        <span class="string">&#x27;feat&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;fix&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;perf&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;style&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;docs&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;test&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;refactor&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;build&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;ci&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;chore&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;revert&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;wip&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;workflow&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;types&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;release&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;temp&#x27;</span></span><br><span class="line">      ],</span><br><span class="line">    ],</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>钩子配置<br>配置完后每次提交代码时，如果未按照规范来提交会校验并报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># --no-install 代表强制使用本地模块</span></span><br><span class="line">npx husky add .husky/commit-msg <span class="string">&quot;npx --no-install commitlint --edit <span class="variable">$1</span>&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="配置Mock"><a href="#配置Mock" class="headerlink" title="配置Mock"></a>配置Mock</h4></li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D mockjs vite-plugin-mock</span><br></pre></td></tr></table></figure></li>
<li><p>返回内容配置<br>在根目录下新建一个mock文件夹，并新建一个user.ts用于用户登录的数据模拟</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; MockMethod &#125; <span class="keyword">from</span> <span class="string">&#x27;vite-plugin-mock&#x27;</span>;</span><br><span class="line"><span class="comment">// url：配置接口URL地址</span></span><br><span class="line"><span class="comment">// method：配置接口的请求方式</span></span><br><span class="line"><span class="comment">// response：编写函数，处理逻辑并返回内容</span></span><br><span class="line"><span class="keyword">const</span> mockList: MockMethod[] = [</span><br><span class="line">  &#123;</span><br><span class="line">    url: <span class="string">&#x27;/mock/api/user&#x27;</span>,</span><br><span class="line">    method: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    <span class="comment">// 使用body可以获取请求体</span></span><br><span class="line">    response: <span class="function">(<span class="params">&#123; body &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (body.username !== body.password) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          code: <span class="number">1</span>,</span><br><span class="line">          message: <span class="string">&#x27;请检查账号/密码&#x27;</span>,</span><br><span class="line">          data: &#123;</span><br><span class="line">            username: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            roles: [],</span><br><span class="line">            accessToken: <span class="string">&#x27;&#x27;</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (body.username === <span class="string">&#x27;admin&#x27;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          code: <span class="number">0</span>,</span><br><span class="line">          message: <span class="string">&#x27;登录成功&#x27;</span>,</span><br><span class="line">          data: &#123;</span><br><span class="line">            username: <span class="string">&#x27;admin&#x27;</span>,</span><br><span class="line">            roles: [<span class="string">&#x27;admin&#x27;</span>],</span><br><span class="line">            accessToken: <span class="string">&#x27;admin&#x27;</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          code: <span class="number">1</span>,</span><br><span class="line">          message: <span class="string">&#x27;Token失效了&#x27;</span>,</span><br><span class="line">          data: &#123;</span><br><span class="line">            username: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            roles: [],</span><br><span class="line">            accessToken: <span class="string">&#x27;&#x27;</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> mockList;</span><br></pre></td></tr></table></figure></li>
<li><p>启用mock<br>需要在vite中配置一下</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 头部引入依赖</span></span><br><span class="line"><span class="keyword">import</span> &#123; viteMockServe &#125; <span class="keyword">from</span> <span class="string">&quot;vite-plugin-mock&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在 plugins 数组中加入如下配置</span></span><br><span class="line">plugins: [</span><br><span class="line">  viteMockServe(&#123;</span><br><span class="line">    <span class="comment">// 如果接口为 /mock/xxx 以 mock 开头就会被拦截响应配置的内容</span></span><br><span class="line">mockPath: <span class="string">&#x27;mock&#x27;</span>, <span class="comment">// 数据模拟需要拦截的请求起始 URL</span></span><br><span class="line">enable: <span class="literal">true</span> <span class="comment">// 本地环境开启mock</span></span><br><span class="line">  &#125;),</span><br><span class="line">]</span><br></pre></td></tr></table></figure></li>
<li><p>关闭mock<br>如果后端接口测试完毕，正式对接时，我们不想启用mock的话，可以使用<strong>环境变量</strong>配置mock的开关</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># .env.development</span></span><br><span class="line"><span class="comment">## axios 请求base地址</span></span><br><span class="line">VITE_APP_API_BASEURL = /api</span><br><span class="line"><span class="comment">## mock 请求根路径</span></span><br><span class="line">VITE_APP_MOCK_BASEURL = /mock/api</span><br><span class="line"><span class="comment">## 是否走mock请求，这里就是mock的开关</span></span><br><span class="line">VITE_APP_USE_MOCK = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># .env.production</span></span><br><span class="line"><span class="comment">## 是否走mock请求，这里就是mock的开关</span></span><br><span class="line">VITE_APP_USE_MOCK = <span class="literal">true</span></span><br></pre></td></tr></table></figure></li>
<li><p><strong>配置axios 全局请求路径</strong></p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> service: AxiosInstance = axios.create(&#123;</span><br><span class="line">  <span class="comment">// 启用 mock 就请求 mock 路径</span></span><br><span class="line">  <span class="comment">// 不启用 mock 就请求 正常后端路径</span></span><br><span class="line">  baseURL: <span class="keyword">import</span>.meta.env.VITE_APP_USE_MOCK</span><br><span class="line">    ? <span class="keyword">import</span>.meta.env.VITE_APP_MOCK_BASEURL</span><br><span class="line">    : <span class="keyword">import</span>.meta.env.VITE_APP_API_BASEURL,</span><br><span class="line">  timeout: <span class="number">5000</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></li>
<li><p>在<code>vite proxy</code>中配置</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 代理服务器</span></span><br><span class="line"><span class="comment">// 帮助我们开发时解决跨域问题</span></span><br><span class="line">proxy: &#123;</span><br><span class="line">  <span class="comment">// 这里的意思是 以/api开头发送的请求都会被转发到 http://localhost:9000</span></span><br><span class="line">  [env.VITE_APP_API_BASEURL]: &#123;</span><br><span class="line">    target: <span class="string">&#x27;http://localhost:9000&#x27;</span>,</span><br><span class="line">    <span class="comment">// 改变 Host Header</span></span><br><span class="line">    changeOrigin: <span class="literal">true</span></span><br><span class="line">    <span class="comment">// 发起请求时将 &#x27;/api&#x27; 替换为 &#x27;&#x27;</span></span><br><span class="line">    <span class="comment">// rewrite: (path) =&gt; path.replace(/^\/api/, &#x27;&#x27;)</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 这里的意思是 以/mock/api开头发送的请求都会被转发到 http://localhost:9000</span></span><br><span class="line">  [env.VITE_APP_MOCK_BASEURL]: &#123;</span><br><span class="line">    target: <span class="string">&#x27;http://localhost:9000&#x27;</span>,</span><br><span class="line">    <span class="comment">// 改变 Host Header</span></span><br><span class="line">    changeOrigin: <span class="literal">true</span>,</span><br><span class="line">    <span class="comment">// 发起请求时将 &#x27;/api&#x27; 替换为 &#x27;&#x27;</span></span><br><span class="line">    <span class="comment">// rewrite: (path) =&gt; path.replace(/^\/api/, &#x27;&#x27;)</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="ElementPlus按需引入"><a href="#ElementPlus按需引入" class="headerlink" title="ElementPlus按需引入"></a>ElementPlus按需引入</h4></li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -D unplugin-auto-import unplugin-element-plus unplugin-icons unplugin-vue-components</span><br></pre></td></tr></table></figure></li>
<li><p>在vite.config.ts新增配置</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> AutoImport <span class="keyword">from</span> <span class="string">&#x27;unplugin-auto-import/vite&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> ElementPlus <span class="keyword">from</span> <span class="string">&#x27;unplugin-element-plus/vite&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> IconsResolver <span class="keyword">from</span> <span class="string">&#x27;unplugin-icons/resolver&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> Icons <span class="keyword">from</span> <span class="string">&#x27;unplugin-icons/vite&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; ElementPlusResolver &#125; <span class="keyword">from</span> <span class="string">&#x27;unplugin-vue-components/resolvers&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> Components <span class="keyword">from</span> <span class="string">&#x27;unplugin-vue-components/vite&#x27;</span>;</span><br><span class="line"></span><br><span class="line"> plugins: [</span><br><span class="line">   ElementPlus(&#123;&#125;),</span><br><span class="line"><span class="comment">// elementplus按需引入</span></span><br><span class="line">AutoImport(&#123;</span><br><span class="line">	resolvers: [ElementPlusResolver(), IconsResolver()],</span><br><span class="line">	<span class="comment">// 将auto-imports.d.ts放到types文件夹下</span></span><br><span class="line">	dts: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&#x27;./types/auto-imports.d.ts&#x27;</span>, <span class="keyword">import</span>.meta.url))</span><br><span class="line">&#125;),</span><br><span class="line">Components(&#123;</span><br><span class="line">	resolvers: [ElementPlusResolver(), IconsResolver()],</span><br><span class="line">	<span class="comment">// 将components.d.ts放到types文件夹下</span></span><br><span class="line">	dts: fileURLToPath(<span class="keyword">new</span> URL(<span class="string">&#x27;./types/components.d.ts&#x27;</span>, <span class="keyword">import</span>.meta.url))</span><br><span class="line">&#125;),</span><br><span class="line">Icons(&#123;</span><br><span class="line">	autoInstall: <span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line"> ]</span><br></pre></td></tr></table></figure>
<h4 id="配置mitt事件总线"><a href="#配置mitt事件总线" class="headerlink" title="配置mitt事件总线"></a>配置mitt事件总线</h4></li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install mitt</span><br></pre></td></tr></table></figure></li>
<li><p>在main.ts中全局配置mitt</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> mitt <span class="keyword">from</span> <span class="string">&#x27;mitt&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> emitter = mitt();</span><br><span class="line"><span class="keyword">declare</span> <span class="built_in">module</span> <span class="string">&#x27;vue&#x27;</span> &#123;</span><br><span class="line">  <span class="keyword">export</span> <span class="keyword">interface</span> ComponentCustomProperties &#123;</span><br><span class="line">    $b: <span class="keyword">typeof</span> emitter;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 事件总线注册</span></span><br><span class="line">app.config.globalProperties.$b = emitter;</span><br></pre></td></tr></table></figure>
<h4 id="配置国际化"><a href="#配置国际化" class="headerlink" title="配置国际化"></a>配置国际化</h4></li>
<li><p>安装依赖</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># vue3最好用9.x以上的版本</span></span><br><span class="line">npm install vue-i18n@9</span><br></pre></td></tr></table></figure>
<h4 id="iconify图标库使用"><a href="#iconify图标库使用" class="headerlink" title="iconify图标库使用"></a>iconify图标库使用</h4></li>
</ul>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/wangbowen-hexo-blog/about" rel="external nofollow noreferrer">W.BOWEN</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://gitee.com/wang-bowen0629/wangbowen-hexo-blog/2024/03/06/cong-0-1-da-jian-yi-ge-vue3-xiang-mu/">https://gitee.com/wang-bowen0629/wangbowen-hexo-blog/2024/03/06/cong-0-1-da-jian-yi-ge-vue3-xiang-mu/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/wangbowen-hexo-blog/about" target="_blank">W.BOWEN</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/wangbowen-hexo-blog/tags/vue/">
                                    <span class="chip bg-color">vue</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/wangbowen-hexo-blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/wangbowen-hexo-blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/wangbowen-hexo-blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/wangbowen-hexo-blog/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/wangbowen-hexo-blog/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/wangbowen-hexo-blog/libs/valine/av-min.js"></script>
<script src="/wangbowen-hexo-blog/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'EDT5zRbrzUXInYp7SCupHY7m-gzGzoHsz',
        appKey: 'u9CyC9PTy66xmdnLhq7Hu8PX',
        notify: 'true' === 'true',
        verify: 'true' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '有好的建议请留言八！'
    });
</script>

<!--酷Q推送-->


    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="far fa-dot-circle"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/wangbowen-hexo-blog/2024/03/06/cong-0-1-da-jian-yi-ge-vue3-xiang-mu/">
                    <div class="card-image">
                        
                        
                        <img src="/wangbowen-hexo-blog/medias/featureimages/1.jpg" class="responsive-img" alt="从0-1搭建一个vue3项目">
                        
                        <span class="card-title">从0-1搭建一个vue3项目</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2024-03-06
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            W.BOWEN
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/wangbowen-hexo-blog/tags/vue/">
                        <span class="chip bg-color">vue</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/wangbowen-hexo-blog/2024/03/06/git-xiang-guan/">
                    <div class="card-image">
                        
                        
                        <img src="/wangbowen-hexo-blog/medias/featureimages/13.jpg" class="responsive-img" alt="git相关">
                        
                        <span class="card-title">git相关</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2024-03-06
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            W.BOWEN
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/wangbowen-hexo-blog/tags/git/">
                        <span class="chip bg-color">git</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/wangbowen-hexo-blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/wangbowen-hexo-blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/wangbowen-hexo-blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/wangbowen-hexo-blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/wangbowen-hexo-blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/wangbowen-hexo-blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='true'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/wangbowen-hexo-blog/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2024</span>
            
            <span id="year">2019</span>
            <a href="/wangbowen-hexo-blog/about" target="_blank">W.BOWEN</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://gitee.com/wang-bowen" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:784801819@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=784801819" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 784801819" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/wangbowen-hexo-blog/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/wangbowen-hexo-blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/wangbowen-hexo-blog/libs/materialize/materialize.min.js"></script>
    <script src="/wangbowen-hexo-blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/wangbowen-hexo-blog/libs/aos/aos.js"></script>
    <script src="/wangbowen-hexo-blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/wangbowen-hexo-blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/wangbowen-hexo-blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/wangbowen-hexo-blog/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/wangbowen-hexo-blog/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/wangbowen-hexo-blog/libs/instantpage/instantpage.js" type="module"></script>
    

<script src="/wangbowen-hexo-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"live2d-widget-model-z16"},"display":{"position":"right","width":200,"height":400},"mobile":{"show":false},"log":false});</script></body>

</html>
